package com.sens.network;

import com.sens.bean.BaseJson;
import com.sens.bean.UserInfo;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.IBasicRequest;
import com.yanzhenjie.nohttp.RequestMethod;

import cryptotools.CryptoTools;

public class BeanRequest<T extends BaseJson> extends BaseBeanRequest<T> {
    private String action;

    public BeanRequest(Class<T> beanClass, String url) {
        super(beanClass, url);
    }

    public BeanRequest(Class<T> beanClass, String url, RequestMethod requestMethod) {
        super(beanClass, url, requestMethod);
    }

    @Override
    public T parseResponse(Headers responseHeaders, byte[] responseBody) {
        T t = super.parseResponse(responseHeaders, responseBody);
        if ("login".equals(action)) {
            UserInfo userInfo = (UserInfo) t;
            ApiByHttp.getInstance().setUserid(userInfo.getUserid());
            String appId =CryptoTools.getUserKey(userInfo.getAppkey(),userInfo.getUserid());
            ApiByHttp.getInstance().setAppid(appId);
        }
        return t;
    }

    @Override
    public IBasicRequest add(String key, String value) {
        if ("action".equals(key)) {
            this.action = value;
        }
        return super.add(key, value);
    }
}
